package jdk;



import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import robocode.control.testing.RobotTestBed;
import robocode.BattleResults;
import robocode.control.events.BattleCompletedEvent;

/**
 * Illustrates JUnit testing of Robocode robots.
 * This test simply verifies that Rasta01 beats Fire at least 20% of the time.
 * 
 * Also illustrates the overriding of a set of methods from RobotTestBed to show how the testing
 * behavior can be customized and controlled. 
 * 
 * @author Philip Johnson - original
 * @author J.D. Korse - derivative
 *
 */
public class TestRasta01VersusFire extends RobotTestBed {
  
  int numRounds;  //  this is set in the Get#Rounds method below.
  int wins;       //  this is set in the BattleCompleted method below.
  boolean winner = false;
  
  /**
   * Specifies that Fire and Rasta01 are to be matched up in this test case.
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.Fire,jdk.Rasta01";
  }
  
  /**
   * This test runs for 10 rounds.
   * @return The number of rounds. 
   */
  @Override
  public int getNumRounds() {
    numRounds = 30;  //  used as a variable in assert test below.
    return 30;
  }
  
  /**
   * The actual test, which asserts that Rasta01 has won at least 20% of the time.
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    // Return the results in order of getRobotNames.
    BattleResults[] battleResults = event.getIndexedResults();
    // Sanity check that results[1] is Rasta01 (not strictly necessary, but illustrative).
    BattleResults Rasta01Results = battleResults[1];
    String robotName = Rasta01Results.getTeamLeaderName();
    assertEquals("Check that results[1] is Rasta01", robotName, "jdk.Rasta01*");
    
    // Check to make sure Rasta01 wins at 20% of the time.    
    wins = Rasta01Results.getFirsts();
    if ((numRounds / 6) <= wins) {
      winner = true;
    }
    assertTrue("Check that Rasta01 wins at least 20% of the time.", winner);
  
  }
}
